/*
 * @(#)AssertionType.java
 *
 *
 *
 * ver 1.0 Aug 16, 2007 ggranum
 */
package org.testfire.model.assertions;

import org.testfire.util.DomUtil;
import org.testfire.model.TestFireObject;
import org.testfire.model.TestsetType;
import org.testfire.translator.TranslatorFactory;
import org.w3c.dom.Element;

public class AssertionType implements TestFireObject
{
    private TestFireObject parent;
    private Assertion assertion;


    public AssertionType( Element element )
    {
        this.parse( element );
    }

    public StringBuilder toCode()
    {
        return toWatijCode();
    }

    private StringBuilder toWatijCode()
    {
        StringBuilder buf = new StringBuilder();

        return buf;
    }

    public void setParent( TestFireObject parent )
    {
        this.parent = parent;
    }

    public TestFireObject getParent()
    {
        return parent;
    }

    public void parse( Element el )
    {
        Element child = DomUtil.getChildElement( el );
        String name = child.getNodeName();
        if(name.endsWith( "Equals" )){
            boolean requiresQuotes = (name.indexOf( "Char") >=0 || name.indexOf( "String") >=0);
            boolean isDouble = name.indexOf( "Double" ) >= 0;
            if(isDouble){
                this.assertion = new AssertDoubleEquals( child );
            }
            else{
                this.assertion = new AssertEquals( child, requiresQuotes );
            }
        }
        else if(name.endsWith( "Same"))
        {
            this.assertion = new AssertSame( child, name.endsWith( "NotSame") );
        }
        else  if(name.endsWith( "Null"))
        {
            this.assertion = new AssertNull( child, name.endsWith( "NotNull") );
        }
        else  if(name.endsWith( "assertTrue"))
        {
            this.assertion = new AssertTrueOrFalse( child, false );
        }
        else  if(name.endsWith( "assertFalse"))
        {
            this.assertion = new AssertTrueOrFalse( child, true );
        }
        else if(name.endsWith( "fail"))
        {
            this.assertion = new AssertFail( child );

        }
    }


    public Object translate( TestsetType testset )
    {
        return TranslatorFactory.translate(testset, this);
    }

	public Assertion getAssertion()
	{
		return assertion;
	}
}

// eof: AssertionType.java